home *** CD-ROM | disk | FTP | other *** search
- /*------------- include header files -------------*/
- #include "WindowExtensions.h"
-
- /*------------- constant for palette window -------------*/
- #define K_ICON_WIDTH 32
- #define K_ICON_HEIGHT 32
-
- #define K_STYLE_CEL_H 1
- #define K_STYLE_CEL_V 3
- #define K_STYLE_ICON_ID 128
- #define K_STYLE_MENU_ITEM 1
-
- #define K_COLOR_CEL_H 3
- #define K_COLOR_CEL_V 2
- #define K_COLOR_ICON_ID 131
- #define K_COLOR_MENU_ITEM 5
-
- #define SETUP_PAL_RECT(r, x, y, w, h) ¥
- SetRect(r, x*w, y*h, (x+1)*w, (y+1)*h)
-
- /*------------- global for program -------------*/
- Boolean exit_program = false;
- MenuHandle apple_menu, file_menu, edit_menu, tool_menu, win_menu;
- AEEventHandlerUPP upp_launch, upp_opendoc, upp_print, upp_quit;
- Rect r_new_window;
- short cur_style = 0, cur_color = 0;
-
- /*------------- global for palette window -------------*/
- CIconHandle cicn_style[K_STYLE_CEL_H * K_STYLE_CEL_V];
- CIconHandle cicn_color[K_COLOR_CEL_H * K_COLOR_CEL_V];
- WindowPtr style_palette, color_palette;
- ActivateHandlerUPP g_active_event_handler_for_doc_window;
-
- /*------------- main -------------*/
- void main(void);
- /*------------- initialize -------------*/
- OSErr install_apple_events_handler(void);
- void do_setup_appl(void);
- void do_init_rom(void);
- /*------------- process apple events -------------*/
- pascal OSErr ae_launch(AppleEvent*ae, AppleEvent*rae, long refcon);
- pascal OSErr ae_opendoc(AppleEvent*ae, AppleEvent*rae, long refcon);
- pascal OSErr ae_print(AppleEvent*ae, AppleEvent*rae, long refcon);
- pascal OSErr ae_quit(AppleEvent*ae, AppleEvent*rae, long refcon);
- /*------------- event loop -------------*/
- void do_main_event_loop(void);
- /*------------- management menus -------------*/
- void setup_checkmark(void);
- void do_menu(long menu_and_item);
- /*------------- control mouse -------------*/
- void do_mouse_down(Point pt);
- /*------------- management document window -------------*/
- void do_update(WindowPtr win);
- void create_new_window(void);
- void close_window(WindowPtr wp);
- /*------------- management palette window -------------*/
- pascal void active_event_handler_for_doc_window(WindowRef win, Boolean activate);
- void create_palette_windows(void);
- void choose_style(int new_style);
- void choose_color(int new_color);
- void click_style_window(Point pt);
- void click_color_window(Point pt);
-
-
- /*------------- main -------------*/
- void main(void)
- {
- do_init_rom();
- install_apple_events_handler();
- do_setup_appl();
- do_main_event_loop();
- }
-
- /*------------- initialize -------------*/
- OSErr install_apple_events_handler(void)
- {
- OSErr err;
- long resp;
- err = Gestalt(gestaltAppleEventsAttr, &resp);
- if((err == noErr) && (resp & (31 - gestaltAppleEventsPresent))) {
- upp_launch = NewAEEventHandlerProc(ae_launch);
- upp_opendoc = NewAEEventHandlerProc(ae_opendoc);
- upp_print = NewAEEventHandlerProc(ae_print);
- upp_quit = NewAEEventHandlerProc(ae_quit);
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, upp_launch, 0, false);
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, upp_opendoc, 0, false);
- err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, upp_print, 0, false);
- err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, upp_quit, 0, false);
- }
- return err;
- }
-
- void do_setup_appl(void)
- {
- Handle h;
- InsertMenu(apple_menu = GetMenu(128), 0);
- InsertMenu(file_menu = GetMenu(129), 0);
- InsertMenu(edit_menu = GetMenu(130), 0);
- InsertMenu(tool_menu = GetMenu(131), 0);
- InsertMenu(win_menu = GetMenu(132), 0);
- AddResMenu(apple_menu, 'DRVR');
- DrawMenuBar();
- g_active_event_handler_for_doc_window =
- NewActivateHandlerProc((ProcPtr)active_event_handler_for_doc_window);
- create_palette_windows();
- SetRect(&r_new_window, 34, 102, 234, 302);
- create_new_window();
- }
-
- void do_init_rom(void)
- {
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- FlushEvents(everyEvent, 0);
- InitCursor();
- }
-
- /*------------- process apple events -------------*/
- pascal OSErr ae_launch(AppleEvent*ae, AppleEvent*rae, long refcon)
- { return noErr; }
- pascal OSErr ae_opendoc(AppleEvent*ae, AppleEvent*rae, long refcon)
- { return errAEEventNotHandled; }
- pascal OSErr ae_print(AppleEvent*ae, AppleEvent*rae, long refcon)
- { return errAEEventNotHandled; }
- pascal OSErr ae_quit(AppleEvent*ae, AppleEvent*rae, long refcon)
- {
- exit_program = true;
- return noErr;
- }
-
- /*------------- event loop -------------*/
- void do_main_event_loop(void)
- {
- EventRecord an_event;
- char ascii_code;
- setup_checkmark();
- do {
- if(WaitNextEvent(everyEvent, &an_event, 0, nil)) {
- switch(an_event.what) {
- case keyDown:
- case autoKey:
- ascii_code = (char)(an_event.message & charCodeMask);
- if((an_event.modifiers & cmdKey) != 0)
- do_menu(MenuKey(ascii_code));
- break;
- case updateEvt:
- do_update((WindowPtr)an_event.message);
- break;
- case mouseDown:
- do_mouse_down(an_event.where);
- break;
- case kHighLevelEvent:
- AEProcessAppleEvent(&an_event);
- break;
- case activateEvt:
- /* never come here. */
- break;
- case osEvt:
- if(((an_event.message >> 24) & 0xFF) == suspendResumeMessage) {
- if(an_event.message & resumeFlag) {
- ResumeFloatingWindows();
- SetCursor(&qd.arrow);
- } else {
- SuspendFloatingWindows();
- }
- }
- break;
- default:
- break;
- }
- setup_checkmark();
- }
- } while(!exit_program);
- }
-
- /*------------- management menus -------------*/
- void setup_checkmark(void)
- {
- int i;
- CheckItem(win_menu, 1, ((WindowPeek)style_palette)->visible);
- CheckItem(win_menu, 2, ((WindowPeek)color_palette)->visible);
- for(i=0; i<(K_STYLE_CEL_H * K_STYLE_CEL_V); i++)
- CheckItem(tool_menu, K_STYLE_MENU_ITEM + i, cur_style == i);
- for(i=0; i<(K_COLOR_CEL_H * K_COLOR_CEL_V); i++)
- CheckItem(tool_menu, K_COLOR_MENU_ITEM + i, cur_color == i);
- }
-
- void do_menu(long menu_and_item)
- {
- short itemnum;
- Str255 da_name;
- itemnum = LoWord(menu_and_item);
- switch(HiWord(menu_and_item)) {
- case 128: // apple menu
- switch(itemnum) {
- case 1:
- break;
- default:
- GetItem(apple_menu, itemnum, da_name);
- OpenDeskAcc(da_name);
- break;
- }
- break;
- case 129: // file menu
- switch(itemnum) {
- case 1: // new window
- create_new_window();
- break;
- case 2: // close window
- close_window(FrontNonFloatingWindow());
- break;
- default:
- exit_program = true;
- break;
- }
- break;
- case 130: // edit menu
- break;
- case 131: // tool menu
- if(itemnum < K_COLOR_MENU_ITEM) {
- choose_style(itemnum - K_STYLE_MENU_ITEM);
- } else {
- choose_color(itemnum - K_COLOR_MENU_ITEM);
- }
- break;
- case 132: // window menu
- switch(itemnum) {
- case 1: // show/hide style palette
- if(((WindowPeek)style_palette)->visible) {
- HideReferencedWindow(style_palette);
- } else {
- ShowReferencedWindow(style_palette);
- }
- break;
- case 2: // show/hide color palette
- if(((WindowPeek)color_palette)->visible) {
- HideReferencedWindow(color_palette);
- } else {
- ShowReferencedWindow(color_palette);
- }
- break;
- }
- break;
- }
- HiliteMenu(0);
- }
-
- /*------------- control mouse -------------*/
- void do_mouse_down(Point pt)
- {
- WindowPtr clicked_wp;
- Rect max_r;
- long new_width_and_height;
- short new_width, new_height;
- GrafPtr saved_port;
- GetPort(&saved_port);
- switch(FindWindow(pt, &clicked_wp)) {
- case inGoAway:
- if(TrackGoAway(clicked_wp, pt)) {
- close_window(clicked_wp);
- }
- break;
- case inDrag:
- max_r = qd.screenBits.bounds;
- InsetRect(&max_r, 8, 8);
- DragReferencedWindow(clicked_wp, pt, &max_r);
- break;
- case inGrow:
- max_r.left = 100;
- max_r.top = 100;
- max_r.right = qd.screenBits.bounds.right - 8;
- max_r.bottom = qd.screenBits.bounds.bottom - 8;
- if(new_width_and_height = GrowWindow(clicked_wp, pt, &max_r)) {
- SetPort(clicked_wp);
- new_width = LoWord(new_width_and_height);
- new_height = HiWord(new_width_and_height);
- SizeWindow(clicked_wp, new_width, new_height, false);
- InvalRect(&clicked_wp->portRect);
- }
- break;
- case inMenuBar:
- do_menu(MenuSelect(pt));
- break;
- case inContent:
- if(clicked_wp == style_palette) {
- click_style_window(pt);
- } else if(clicked_wp == color_palette) {
- click_color_window(pt);
- } else if(clicked_wp != FrontNonFloatingWindow()) {
- SelectReferencedWindow(clicked_wp);
- }
- break;
- default:
- break;
- }
- SetPort(saved_port);
- }
-
- /*------------- management document window -------------*/
- void do_update(WindowPtr win)
- {
- GrafPtr saved_port;
- Rect r;
- RGBColor rgb;
- int x, y, i;
- long refcon;
- GetPort(&saved_port);
- SetPort(win);
- BeginUpdate(win);
- if(win == style_palette) {
- PenSize(4, 4);
- for(y=0, i=0; y<K_STYLE_CEL_V; y++)
- for(x=0; x<K_STYLE_CEL_H; x++, i++){
- SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
- PlotCIcon(&r, cicn_style[i]);
- if(i == cur_style) FrameRect(&r);
- }
- PenSize(1, 1);
- } else if(win == color_palette) {
- PenSize(4, 4);
- for(y=0, i=0; y<K_COLOR_CEL_V; y++)
- for(x=0; x<K_COLOR_CEL_H; x++, i++) {
- SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
- PlotCIcon(&r, cicn_color[i]);
- if(i == cur_color) FrameRect(&r);
- }
- PenSize(1, 1);
- } else {
- EraseRect(&win->portRect);
- r = win->portRect;
- r.right -= 14;
- r.bottom -= 14;
- InsetRect(&r, 16, 16);
- refcon = GetExtWRefCon(win);
- switch(refcon % 256) {
- case 0: ForeColor(redColor); break;
- case 1: ForeColor(greenColor); break;
- case 2: ForeColor(blueColor); break;
- case 3: ForeColor(yellowColor); break;
- case 4: ForeColor(magentaColor); break;
- case 5: ForeColor(blackColor); break;
- }
- switch(refcon >> 8) {
- case 0: PaintOval(&r); break;
- case 1: PaintRect(&r); break;
- case 2: PaintRoundRect(&r,
- (r.right - r.left) / 6,
- (r.bottom - r.top) / 6); break;
- }
- ForeColor(blackColor);
- DrawGrowIcon(win);
- }
- EndUpdate(win);
- SetPort(saved_port);
- }
-
- void create_new_window(void)
- {
- WindowPtr win;
- OffsetRect(&r_new_window, 10, 16);
- if(r_new_window.bottom > qd.screenBits.bounds.bottom)
- OffsetRect(&r_new_window, 0, - r_new_window.top + 50);
- if(r_new_window.right > qd.screenBits.bounds.right)
- OffsetRect(&r_new_window, - r_new_window.left + 10, 0);
- if(NewWindowReference(
- &win,
- &r_new_window,
- "¥pUntitled",
- true,
- (kHasCloseBoxMask | kHasDocumentTitlebarMask | kHasGrowBoxMask),
- (void*)-1,
- 0,
- active_event_handler_for_doc_window)) {
- ExitToShell();
- }
- SetExtWRefCon(win, 0);
- }
-
- void close_window(WindowPtr win)
- {
- if(win == style_palette) {
- HideReferencedWindow(style_palette);
- } else if(win == color_palette) {
- HideReferencedWindow(color_palette);
- } else if(win) {
- DisposeWindowReference(win);
- }
- }
-
- /*------------- management palette window -------------*/
- pascal void active_event_handler_for_doc_window(WindowRef win, Boolean activate)
- {
- GrafPtr saved_port;
- long refcon;
- GetPort(&saved_port);
- SetPort((GrafPtr) win);
- DrawGrowIcon(win);
- if(activate) {
- refcon = GetExtWRefCon(win);
- choose_style(refcon / 256);
- choose_color(refcon % 256);
- } else {
- SetPort(saved_port);
- }
- }
-
- void create_palette_windows(void)
- {
- Rect r;
- int x, y, i;
- SetRect(&r, 4,
- 40,
- 4 + K_ICON_WIDTH * K_STYLE_CEL_H,
- 40 + K_ICON_HEIGHT * K_STYLE_CEL_V);
- if(NewWindowReference(
- &style_palette,
- &r,
- "¥pStyle",
- true,
- (kHasCloseBoxMask | kHasPaletteTitlebarMask),
- (void*)-1,
- 0,
- nil)) {
- ExitToShell();
- }
- SetRect(&r, r.right + 20,
- 28,
- r.right + 20 + K_ICON_WIDTH * K_COLOR_CEL_H,
- 28 + K_ICON_HEIGHT * K_COLOR_CEL_V);
- if(NewWindowReference(
- &color_palette,
- &r,
- "¥pColor",
- true,
- (kHasCloseBoxMask | kHasSideTitleBarMask | kHasPaletteTitlebarMask),
- (void*)-1,
- 0,
- nil)) {
- ExitToShell();
- }
- for(y=0, i=0; y<K_STYLE_CEL_V; y++)
- for(x=0; x<K_STYLE_CEL_H; x++, i++)
- cicn_style[i] = GetCIcon(K_STYLE_ICON_ID + i);
- for(y=0, i=0; y<K_COLOR_CEL_V; y++)
- for(x=0; x<K_COLOR_CEL_H; x++, i++)
- cicn_color[i] = GetCIcon(K_COLOR_ICON_ID + i);
- }
-
- void choose_style(int new_style)
- {
- WindowPtr win;
- GrafPtr saved_port;
- long refcon;
- GetPort(&saved_port);
- win = FrontNonFloatingWindow();
- if(cur_style != new_style) {
- if(win) {
- refcon = (GetExtWRefCon(win) & 0x000000ff) | (new_style << 8);
- SetExtWRefCon(win, refcon);
- SetPort(win);
- InvalRect(&win->portRect);
- }
- cur_style = new_style;
- SetPort(style_palette);
- InvalRect(&style_palette->portRect);
- }
- SetPort(saved_port);
- }
-
- void choose_color(int new_color)
- {
- WindowPtr win;
- GrafPtr saved_port;
- long refcon;
- GetPort(&saved_port);
- win = FrontNonFloatingWindow();
- if(cur_color != new_color) {
- if(win) {
- refcon = (GetExtWRefCon(win) & 0x0000ff00) | (new_color);
- SetExtWRefCon(win, refcon);
- SetPort(win);
- InvalRect(&win->portRect);
- }
- cur_color = new_color;
- SetPort(color_palette);
- InvalRect(&color_palette->portRect);
- }
- SetPort(saved_port);
- }
-
- void click_style_window(Point pt)
- {
- Rect r;
- int x, y, i;
- GrafPtr saved_port;
- GetPort(&saved_port);
- SetPort(style_palette);
- GlobalToLocal(&pt);
- for(y=0, i=0; y<K_STYLE_CEL_V; y++)
- for(x=0; x<K_STYLE_CEL_H; x++, i++) {
- SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
- if(PtInRect(pt, &r)) {
- choose_style(i);
- break;
- }
- }
- SetPort(saved_port);
- }
-
- void click_color_window(Point pt)
- {
- Rect r;
- int x, y, i;
- GrafPtr saved_port;
- GetPort(&saved_port);
- SetPort(color_palette);
- GlobalToLocal(&pt);
- for(y=0, i=0; y<K_COLOR_CEL_V; y++)
- for(x=0; x<K_COLOR_CEL_H; x++, i++) {
- SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
- if(PtInRect(pt, &r)) {
- choose_color(i);
- break;
- }
- }
- SetPort(saved_port);
- }
-